<template>
    <!-- 逾期天数冲正申请 -->
    <el-dialog :title="$t('finance.overdueDaysApply')" :visible.sync="dialogApplyCorrection" @close="close" width="550px">
        <el-form :model="formApplyCorrection">
            <el-form-item :label="$t('common.borrowerName') + ':'" :label-width="formLabelWidth"><!--借款人姓名-->
                {{formApplyCorrection.userName}}
            </el-form-item>
            <el-form-item :label="$t('common.innerOrderId') + ':'" :label-width="formLabelWidth"><!--内部订单号-->
                <el-input v-model="formApplyCorrection.orderId" auto-complete="off" readonly></el-input>
            </el-form-item>
            <el-form-item :label="$t('finance.overdueCorrection') + ':'" :label-width="formLabelWidth"><!--逾期冲正天数-->
                <el-input v-model="formApplyCorrection.correctDay" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item :label="$t('finance.overdueCorrectionType') + ':'" :label-width="formLabelWidth"><!--逾期天数冲正类型-->
                <el-select v-model="formApplyCorrection.correctReason" :placeholder="$t('verify.plsChoose')">
                    <el-option :label="$t('finance.errorOverdueCorrection')" value="异常逾期天数冲正"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="close">{{ $t('common.cancel') }}</el-button>
            <el-button type="primary" @click="submitApplyCorrection">{{ $t('common.confirm') }}</el-button>
        </div>
    </el-dialog>
    <!-- 逾期天数冲正申请 -->
</template>

<script>
    export default {
        name: 'apply-correction',
        props: {
            visible: {
                type: Boolean,
                required: true,
            },
            orderInfo: {
                type: Object,
                required: true,
            }
        },
        data() {
            return {
                formApplyCorrection: {
                    name: '',
                    orderId: '',
                    correctDay: '',
                    correctReason: this.$t('finance.errorOverdueCorrection')
                },
                dialogApplyCorrection: false,
                formLabelWidth: this.$storage.get('language') === 'th' ? '220px' : '130px',
            };
        },
        watch: {
            visible(val) {
                if (val) {
                    this.dialogApplyCorrection = val;
                    this.initData();
                }
            },
        },
        methods: {
            submitApplyCorrection() {
                const { correctDay, lateDays, userId, orderId, correctReason } = this.formApplyCorrection;
                if (!/^(?!0\d*$)\d+$/.test(correctDay)) {
                    this.$message.error(this.$t('verify.pleaseEnterCorrectDay')); // 请输入正确的冲正天数
                    return;
                }
                //如订单已经进行 金额减免操作（无论审核通过还是未通过都不可以操作天数冲正）
                if (parseInt(correctDay) > parseInt(lateDays)) {
                    this.$message.error(this.$t('finance.correctionCanNotMoreOverdueDay'));//冲正天数不可大于逾期天数
                } else {
                    let params = {
                        userId,
                        orderId,
                        correctReason,
                        correctDay,
                    };
                    this.$api.finance.repay.addRepayCorrect(params).then(res => {
                        const {status, error} = res;
                        if ('1' === status && '00000000' === error) {
                            this.close('updateData');
                        }
                    });
                }

            },
            initData() {
                const orderInfo = this.orderInfo;
                if (orderInfo.lateDays < 1 || !orderInfo.lateDays) return;
                this.formApplyCorrection = Object.assign({
                    name: '',
                    orderId: '',
                    correctDay: '',
                    correctReason: this.$t('finance.errorOverdueCorrection')
                }, orderInfo);
                this.formApplyCorrection.correctDay = '';
            },
            close(type) {
                if ('updateData' === type) {
                    this.$emit('updateData');
                }
                this.dialogApplyCorrection = false;
                this.$emit('update:visible', false);
            }
        },
        created() {
            this.initData();
            this.dialogApplyCorrection = this.visible;
        },
    }
</script>

<style scoped>

</style>